package com.uaroadshow.finale.web.controller.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.uaroadshow.entity.trade.Order;
import com.uaroadshow.entity.user.User;
import com.uaroadshow.finale.entity.Shopping;
import com.uaroadshow.finale.service.DataService;
import com.uaroadshow.finale.service.OrderService;

/**
 * 订单
 * 
 * @author mohanlan
 *
 */
@RestController
@RequestMapping("/api/orders")
public class OrderApiController {
    
    private static final Logger logger = LoggerFactory.getLogger(OrderApiController.class);
    
    @Autowired
    private OrderService orderService;
    
    @Autowired
    @Qualifier("dataService")
    private DataService<User> userService;
    
    /**
     * 创建订单
     * 
     * @param unionid
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult create(@RequestParam("unionid") String unionid,
                             Shopping shopping) {
        
        HttpResult httpResult = new HttpResult();
        
        try {

            User user = userService.load(User.class, "unionid", unionid);
            if (user == null) {
                throw BusinessException.newInstance("500", "用户不存在");
            }
            
            Order order = orderService.create(shopping, user);
            
            httpResult.success().data("order", order)
                                .data("product", order.getProduct())
                                .data("user", order.getUser())
                                .data("userInfo", order.getUserInfo())
                                .message("创建订单成功");
            
        } catch (BusinessException e) {
            logger.error("#create() - creat order fail， business exception=[{}]", e);
            httpResult.fail(e.getCode()).message(e.getMessage());
        } catch (Exception e) {
            logger.error("#create() - creat order fail，exception=[{}]", e);
            httpResult.fail("500").message("创建订单失败");
        }
        
        return httpResult;
        
    }
    
    /**
     * 订单详情
     * 
     * @param unionid
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult view(@RequestParam("unionid") String unionid,
                           @PathVariable("id") Long id) {
        
        HttpResult httpResult = new HttpResult();
        
        try {

            Order order = orderService.load(Order.class, id);
            
            httpResult.success().data("order", order)
                                .data("product", order.getProduct())
                                .data("user", order.getUser())
                                .data("userInfo", order.getUserInfo())
                                .message("查询成功");
            
        } catch (Exception e) {
            logger.error("#view() - get order fail，exception=[{}]", e);
            httpResult.fail("500").message("查询失败");
        }
        
        return httpResult;
        
    }
}
